package pile.pay.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration // 声明该类为配置类，相当于传统的XML配置文件
@EnableAsync // 开启异步处理
public class AsyncConfig implements AsyncConfigurer { // 实现AsyncConfigurer接口，用于配置异步处理

    @Bean(name = "asyncExecutor") // 声明一个bean，名为asyncExecutor
    public Executor asyncExecutor() { // 定义一个名为asyncExecutor的线程池bean
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 创建ThreadPoolTaskExecutor对象
        executor.setCorePoolSize(10); // 设置核心线程池大小
        executor.setMaxPoolSize(128); // 设置最大线程池大小
        executor.setQueueCapacity(1000); // 设置队列容量
        executor.setThreadNamePrefix("MyAsyncThread-"); // 设置线程名前缀为"MyAsyncThread-"
        executor.initialize(); // 初始化线程池
        return executor; // 返回线程池bean
    }
}